package com.example.test0125;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Controller
@RequestMapping("T")
public class Test {
    @ResponseBody
    @RequestMapping("r1")
    public String func1(HttpServletRequest request, @SessionAttribute("name" )String session){


        if("xiaobi".equals(session)){
            return "欢迎您的到来";
        }else return "失败了";
    }

    @ResponseBody

    @RequestMapping("r2")
    public String func2(String name,HttpSession session){
      session.setAttribute("name","xiaobi");
        session.setAttribute("password","123456");

        return name+"你好";

    }
    @ResponseBody

    @RequestMapping("r3")
    public String func3(MultipartFile file) throws IOException {

        System.out.println("获取源文件的名称为："+file.getOriginalFilename());

        file.transferTo(new File("D:\\"+file.getOriginalFilename()));

        return "OK";

    }

    @ResponseBody
    @RequestMapping("/r4/{type}/{bookname}")
    public String func4(@PathVariable Integer type,@PathVariable() String bookname){

        return "书本类型为"+type+"  书本名字为"+bookname;

    }

    @RequestMapping("r5")
    public String func5(){
        return "/index1.html";
    }

    @ResponseBody
    @RequestMapping(value = "/sum",produces="text/plaim")
    public String func6(int num1,int num2){
        int n=num1+num2;

        return "<h1>计算出来的结果是:"+n+"<h1>";
    }

}
